package com.rwy.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "hash")
public class JsonObject implements Serializable {

	private static final long serialVersionUID = -2744990363853889888L;

	public static final String STATUS_SUCCESS = "success";
	public static final String STATUS_FAIL = "fail";
	public static final String STATUS_ERROR = "error";
	public static final String STATUS_DENY = "deny";
	public static final String NOT_ALLOW_CLICK_INCR = "noincr";
	public static final String STATUS_UN_LOGIN = "unlogin";
	public static final String STATUS_EXCEPTION = "exception";
	public static final String STATUS_NEED_UPDATE = "needUpdate";
	public static final String STATUS_PARAM_EXCEPTION="param_exception"; 	//参数异常

	public static final String NO_DATA_EXCEPTION="no_found_data"; 	//没有找到数据
	//数据字段验证错误，插入或更新失败。
	public static final String DATA_VALIDATION_FIELD = "3001";
	public static final String DATA_VALIDATION_FIELD_MSG = "Field validation fail";
	//数据已存在验证错误，插入或更新失败。
	public static final String DATA_VALIDATION_EXIST = "3002";
	public static final String EXISTS_DATA_EXCEPTION="Data exist exception"; 	//参数异常
	//删除失败：数据已被引用
	public static final String DATA_VALIDATION_CONSTRAINT = "3003";
	public static final String DATA_VALIDATION_CONSTRAINT_MSG = "Delete fail: Data Constraint";
	//数据操作失败：请求未被授权--非法操作不是自己的数据
	public static final String DATA_VALIDATION_UNAUTHORIZED = "3004";
	public static final String DATA_VALIDATION_UNAUTHORIZED_MSG = "Request not authorized";
	
	public static final String DATA_VALIDATION_SENSITIVE_WORD = "3005";
	
	public static final String DATA_VALIDATION_SENSITIVE_WORD_MSG = "sensitive word";
	
	public static final String DATA_VALIDATION_SAME_PRODUCTNAME_MODEL = "3006";

	public static final String DATA_VALIDATION_SAME_PRODUCTNAME_MODEL_MSG = "same productName and model";

	public static final String SYSTEM_EXCEPTION="System error, please contact customer service"; 	//参数异常
	
	public static final String TYPE_HTML = "html";
	public static final String TYPE_JSON = "json";

	private String status = Status.STATUS_SUCCESS;

	private String errorCode;
	
	private String message = "";

	private String type;

	private Object data;
	
	@SuppressWarnings("rawtypes")
	private PageInfo page = new PageInfo();

	public JsonObject() {
		super();
	}
	
	public static JsonObject getInstance() {
		return new JsonObject();
	}
	
	public JsonObject(String status, String errorCode, String message, String type, Object data, @SuppressWarnings("rawtypes") PageInfo page) {
		super();
		if (null != status) {
			this.status = status;			
		}
		this.errorCode = errorCode;
		if (null != message) {
			this.message = message;			
		}
		this.type = type;
		this.data = data;
		this.page = page;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@SuppressWarnings("rawtypes")
	public PageInfo getPage() {
		return page;
	}

	public void setPage(@SuppressWarnings("rawtypes") PageInfo page) {
		this.page = page;
	}
	
	public void setErrorMessage(String message){
		this.setMessage(message);
		this.setStatus(Status.STATUS_ERROR);
	}
	
	public void setSuccessMessage(String message) {
		this.setMessage(message);
		this.setStatus(Status.STATUS_SUCCESS);
	}

	public Map<String, Object> toMap() {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("data", PojoUtil.translate(this.data));
		result.put("status", this.status);
		result.put("type", type);
		result.put("page", this.page);
		result.put("message", this.message);
		return result;
	}
	
	public void setFailMessage(String message) {
		this.setMessage(message);
		this.setStatus(JsonObject.STATUS_FAIL);
	}
	
	@Override
	public String toString() {
		return "JsonObject [status=" + status + ", type=" + type + ", data="
				+ data + ", page=" + page + "]";
	}

	
}
